package cn.tedu.straw.search.service.impl;

import cn.tedu.straw.search.mapper.QuestionMapper;
import cn.tedu.straw.search.repository.QuestionRepository;
import cn.tedu.straw.search.service.IQuestionService;
import cn.tedu.straw.search.vo.QuestionVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Value("${project.question.query-from-database.page-size}")
    private int queryPageSize;

    @Override
    public PageInfo<QuestionVO> getQuestionsFromDataBase(int pageNum) {
        PageHelper.startPage(pageNum, queryPageSize);
        List<QuestionVO> questions = questionMapper.findQuestions();
        return new PageInfo<>(questions);
    }

    @Value("${project.question.query-from-es.page-size}")
    private int searchPageSize;
    @Autowired
    private QuestionRepository questionRepository;

    @Override
    public Page<QuestionVO> search(String keyword, int pageNum) {
        // TODO PageRequest和笔记不一样
        Pageable pageable = PageRequest.of(pageNum - 1, searchPageSize);
        Page<QuestionVO> page =
                questionRepository.queryByTitleMatchesOrContentMatches(keyword,keyword,pageable);
        return page;
    }
}
